<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
<table border="1">
  <caption>商品列表</caption>
  <tr><th>id</th><th>标题</th><th>价格</th><th>库存</th><th>操作</th></tr>
  <tr v-for="p in arr">
    <td>{{p.id}}</td><td>{{p.title}}</td><td>{{p.price}}</td><td>{{p.num}}</td>
      <!--javascript:void(0)禁用超链接的跳转-->
      <td>
          <a :href="'/update.html?id='+p.id">修改</a>
          <a href="javascript:void(0)" @click="del(p.id)">删除</a>
      </td>
  </tr>
</table>
</div>

<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
          arr:[{id:1,title:"冰红茶",price:5,num:100},
              {id:2,title:"绿茶",price:4,num:200},
              {id:3,title:"果粒橙",price:3,num:300}]
        },
        methods:{
          del(id){
              if (confirm("您确认删除此商品吗?")){
                  //发出异步post
                  //axios.post("/delete",{id:id}) 此时接收也要一个对象，此方式很少用
                  axios.post("/delete?id="+id).then(function () {
                      location.reload();//刷新页面
                  })
              }
          }
        },
        created:function () {
          //此方法是Vue对象实例化过程中调用的方法,进入页面立即发请求的代码写在此处
          axios.get("/select").then(function (response) {
            //服务器返回的是装着多个商品对象的数组
            v.arr = response.data;
          })
        }
    })
</script>
</body>
</html>